<template>
  <div class="dfashboard-container">
    <div class="app-container">
      <!-- tab切换 -->
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-button type="primary" size="small" @click="$refs.addRole.dialogShow = true">新增角色</el-button>
          <br><br>
          <!-- 表格 -->
          <el-table border stripe :data="tableData" style="width: 100%">
            <el-table-column type="index" label="序号" width="180" />
            <el-table-column prop="name" label="角色名称" width="180" />
            <el-table-column prop="description" label="角色描述" />
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button size="small" type="success" @click="openAuth(scope.row.id)">分配权限</el-button>
                <el-button size="small" type="primary" @click="$refs.addRole.getRoleInfo(scope.row.id)">编辑</el-button>
                <el-button size="small" type="danger" @click="delRow(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页组件 -->
          <el-pagination
            :current-page.sync="queryData.page"
            :page-sizes="[10, 40, 80, 100]"
            :page-size="queryData.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            background
            @size-change="sizeChange"
            @current-change="pageChange"
          >
            :pager-count="7">
          </el-pagination>

        </el-tab-pane>
        <el-tab-pane label="公司信息">
          <el-alert
            title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
            type="info"
            show-icon
            :closable="false"
          />
          <el-form label-width="120px" style="margin-top:50px">
            <el-form-item label="公司名称">
              <el-input v-model="companyInfo.name" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="companyInfo.companyAddress" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="companyInfo.mailbox" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="companyInfo.remarks" type="textarea" :rows="3" disabled style="width:400px" />
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <!-- 公司信息 -->
      </el-tabs>
    </div>
    <!-- 新增修改组件 -->
    <AddRole ref="addRole" @updateList="initData" />
    <SetAuth ref="setAuth" />
  </div>
</template>

<script>
import SetAuth from '@/views/setting/components/setAuth.vue'
import AddRole from '@/views/setting/components/addRole.vue'
import { getRoleListApi, getCompanyInfoApi, delRoleApi } from '@/api/setting'
import { mapState } from 'vuex'
export default {
  components: {
    AddRole,
    SetAuth
  },
  data() {
    return {
      tableData: [],
      queryData: {
        page: 1,
        pagesize: 10
      },
      total: 0,
      companyInfo: {}
    }
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  created() {
    this.initData()
    this.getCompanyData()
  },
  methods: {
    openAuth(id) {
      this.$refs.setAuth.dialogShow = true
      this.$refs.setAuth.getAllAuthList()
      this.$refs.setAuth.getCurRoleInfo(id)
    },
    async initData() {
      const { rows, total } = await getRoleListApi(this.queryData)
      // console.log(res)
      this.tableData = rows
      this.total = total
    },
    async getCompanyData() {
      this.companyInfo = await getCompanyInfoApi(this.userInfo.companyId)
    },
    sizeChange(val) {
      console.log(val)
      this.queryData.pagesize = val
      this.initData()
    },
    pageChange(val) {
      // console.log(val)
      this.queryData.page = val
      this.initData()
    },
    async delRow(id) {
      try {
        await this.$confirm('确定删除吗？')
      } catch (error) {
        return console.log(error)
      }
      await delRoleApi(id)
      // 提示
      this.$message.success('删除成功！')
      // 边缘问题：如果不是第一页
      if (this.queryData.page > 1 && this.tableData.length === 1) {
        this.queryData.page--
      }
      // 更新
      this.initData()
    }

  }
}
</script>

<style></style>
